package zuosheng.stackandQueue;

import java.util.Stack;

/**
 * @author lj
 * @Description: 用栈实现另外一个栈的排序
 * @date 2020-11-14 13:38
 * @QQ 851477108
 */


public class StackStackSort {

    public static void SortStackByStack(Stack<Integer> stack){
        Stack<Integer> help = new Stack<>();
        while (!stack.isEmpty()){
            int cur = stack.pop();
            //如果cur大于help的栈顶元素，则将help元素小于的依次弹出，逐一压入stack中
            while (!help.isEmpty() && help.peek() < cur){
                stack.push(help.pop());
            }
            //直到cur小于或者等于help的栈顶元素，再将cur压入help中
            help.push(cur);
        }

        //将help已经排好序的元素重新压入stack中
        while (!help.isEmpty()){
            stack.push(help.pop());
        }
    }
}
